import { request as http } from '@/api/service'

import { useUserStore } from '@/store/user'

import IBaseObj from '#/index'

const { uid } = useUserStore().userInfo

const base = '/ctms/project/'

// const getProjectByPage = (data: IBaseObj) =>
//   http({
//     url: `${base}page`,
//     method: 'post',
//     data,
//   })

const saveProject = (data: IBaseObj) =>
  http({
    url: `${base}save`,
    method: 'post',
    data,
  })

const getProjectByUid = (uid: string) =>
  http({
    url: `${base}list/${uid}`,
    method: 'get',
  })

// 影像库研究中心
const getProjectCenterByUid = (uid: string) =>
  http({
    url: `${base}list/center/${uid}`,
    method: 'get',
  })

const foucs = (pid: string | number, uid: string | number) =>
  http({
    url: `${base}focus/${pid}/${uid}`,
    method: 'get',
  })

const unfoucs = (pid: string | number, uid: string | number) =>
  http({
    url: `${base}unfocus/${pid}/${uid}`,
    method: 'get',
  })

const getProjectList = (data: IBaseObj) =>
  http(
    {
      url: `${base}list`,
      method: 'post',
      data,
    },
    true
  )

const getProjectCount = (uid: string | number) =>
  http(
    {
      url: `${base}count/${uid}`,
      method: 'get',
    },
    true
  )

const getFocusProjectCount = (uid: string | number) =>
  http(
    {
      url: `${base}count/${uid}/focus`,
      method: 'get',
    },
    true
  )

const delUrl = '/ctms/project'

/* team */
const teamUrl = `${base}team`

const getTeamByPage = (data: IBaseObj) =>
  http({
    url: `${teamUrl}/page`,
    method: 'post',
    data,
  })

const getTeamList = (data: IBaseObj) =>
  http(
    {
      url: `${teamUrl}/list`,
      method: 'post',
      data,
    },
    true
  )

const getTeamCount = (pid: string | number) =>
  http(
    {
      url: `${teamUrl}/count/${pid}`,
      method: 'get',
    },
    true
  )

const addTeamUser = (data: IBaseObj) =>
  http(
    {
      url: `${teamUrl}/add`,
      method: 'post',
      data,
    },
    true
  )

const updateTeamUser = (data: IBaseObj) =>
  http(
    {
      url: `${teamUrl}/update`,
      method: 'post',
      data,
    },
    true
  )
const delTeamUrl = teamUrl + '/remove'

/** new */

const getProjectById = (pid: string) =>
  http({
    url: `/project/${pid}`,
    method: 'get',
  })

const getProjectByPage = (data: IBaseObj) =>
  http({
    url: `/project/query`,
    method: 'post',
    data,
  })

export const createProject = (data: IBaseObj) =>
  http({
    url: `/project/create`,
    method: 'post',
    data: {
      ...data,
      uid,
    },
  })

export const updateProject = (data: IBaseObj) =>
  http({
    url: `/project/modify`,
    method: 'put',
    data: {
      ...data,
    },
  })

export const getProjectMenu = (data: IBaseObj) =>
  http({
    url: `/project/menu`,
    method: 'get',
    params: data,
  })

const saveProjectOrg = (data: IBaseObj[]) =>
  http({
    url: `/project/org/save/batch`,
    method: 'post',
    data,
  })

const queryProjectOrg = (data: IBaseObj) =>
  http({
    url: `/project/org/query`,
    method: 'post',
    data,
  })

const delOrg = '/project/org'

const saveProjectMember = (data: IBaseObj[]) =>
  http({
    url: `/project/org/save/batch`,
    method: 'post',
    data,
  })

const queryProjectMember = (data: IBaseObj) =>
  http({
    url: `/project/member/query`,
    method: 'post',
    data,
  })

const updateMemberRole = (data: { id: number; rid: number }) => {
  http({
    url: `/project/member/modify/role`,
    method: 'put',
    data,
  })
}

const delMember = '/project/member'

export default {
  saveProject,
  getProjectById,
  getProjectByUid,
  getProjectCenterByUid,
  delUrl,
  foucs,
  unfoucs,
  getProjectList,
  getProjectCount,
  getFocusProjectCount,
  getTeamByPage,
  getTeamCount,
  addTeamUser,
  updateTeamUser,
  delTeamUrl,
  getTeamList,

  getProjectByPage,
  createProject,
  getProjectMenu,
  updateProject,
  saveProjectOrg,
  queryProjectOrg,
  delOrg,
  saveProjectMember,
  queryProjectMember,
  updateMemberRole,
  delMember,
}
